home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Libraries
/
obi stuff
/
demo.obi
< prev
next >
Wrap
Text File
|
1986-04-19
|
3KB
|
168 lines
/*
* This is the OBJECT class. Its the root of
* all objects.
*/
/*< class OBJECT >*/
/*< super NULL >*/
/*< defines >*/
char *malloc();
/*< variables class >*/
/*< variables instance >*/
/*< methods class >*/
/*
* Dynamically create a new object.
*/
object Generic *
new()
{
object Generic *ptr;
/* get memory for the requested object */
ptr = (object Generic *) malloc(self.size);
/* make the memory into and object */
objectify => self(ptr);
return ptr;
}
/*
* Convert memory into a requested Instance object.
*/
objectify(ptr)
object Generic *ptr;
{
ptr->classType = self.classType;
ptr->superType = self.superType;
ptr->size = self.size;
ptr->funcTbl = InstTbl;
}
/*< methods instance >*/
/* None. */
/*< class TEST >*/
/*< super OBJECT >*/
/*< defines >*/
/*< variables class >*/
/*< variables instance
int value;
>*/
/*< methods instance >*/
/*
* Print the value in the current instance object.
*/
print()
{
printf(" %d\n", self.value);
}
/*
* Set the Instance variable value.
*/
set(value)
int value;
{
self.value = value;
}
/*
* Returns the Instance variable value in the
* current instance object.
*/
view()
{
return self.value;
}
%%%% /* start C code */
main()
{
/*
* Remember, test1 is a pointer to ans object and
* therefore has no actual storage allocation for it
* while test2 has storage for an object already
* allocated and ready to use.
*/
object TEST *test1, test2;
/*
* The message "new" is sent to the Class object
* "TEST" which dynamically creates a new instance
* object for the Class TEST (it used the method
* "new" from it's superclass OBJECT). Notice the
* return value is cast into the desired type.
*/
test1 = (object TEST *) new => TEST();
/*
* The message "objectify" sent to the Class object
* "TEST" with a pointer to storage large enough to
* hold an instance object of TEST Class, turns the
* raw memory into an instance object of type TEST.
*/
objectify => TEST(&test2);
/*
* All references to an object in a message must refer
* to the object itself. So in the case where you have
* a pointer to an object the pointer must be dereferenced.
*/
set => *test1(12);
/*
* Since this is the object (not a pointer to one)
* no dereference is necessary.
*/
set => test2(13);
printf("test1 "); print => *test1();
printf("test2 "); print => test2();
/*
* This is to show what happens when you send a message
* to an object that it doesn't know about. In this case
* I will send the Class method message "new" to an instance
* object.
*/
new => *test1();
}
/*
* This function is executed when a message is sent
* to an object that it doesn't know about.
*/
doesNotRespond(obj, meth)
object Generic *obj;
int meth;
{
/* DEBUG is set by the -d flag */
#ifdef DEBUG
printf("object \"%s\" doesn't respond to \"%s\" commmand\n",
className[obj->classType], methName[meth]);
#else
printf("object %x does not respond\n", obj->classType);
#endif
exit();
}